package com.mybatis.dao;

import com.mybatis.pojo.Student;
import com.mybatis.util.mybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.apache.log4j.Logger;
import java.util.List;


public class StudentDaotext {
    static Logger logger = Logger.getLogger(StudentDaotext.class);
//    查询 所有学生
    @Test
    public void test(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper studentdao = sqlSession.getMapper(Mapper.class);
        List<Student> studentList = studentdao.getStudent();
        for( Student student :studentList){
            System.out.println(student);
        }
        sqlSession.close();
    }
    //根据ID查询学生
    @Test
    public  void getStudentId(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        Student student = mapper.getStudentId(1);
        System.out.println(student);
        sqlSession.commit();
        sqlSession.close();
    }
    //增加一名学生
    @Test
    public void addStudent(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        int in = mapper.addStudent(new Student(1,"啦啦",18,"软件技术"));
        if(in>0){
            System.out.println("增加成功");
        }else {
            System.out.println("增加失败");

        }
        sqlSession.commit();
        sqlSession.close();
    }
//    修改学生信息
    @Test
    public void updateStudent(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        int in = mapper.updateStudent(new Student(10,"图图",19,"前端"));
        sqlSession.commit();
        sqlSession.close();
    }
    //    删除学生信息
    @Test
    public void deletStudent(){
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        mapper.deletStudent(9);
        sqlSession.commit();
        sqlSession.close();
    }}



